package jsyf.oa.web.doc;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.doc.MainFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2018/4/20.
 */
@Api(value = "文档Main接口", description = "文档Main接口")
@Controller
@RequestMapping("/docMain")
public class MainController {

    @Resource
    private MainFacade mainFacade;

    @RequestMapping(value = "/getMainByPage.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取全部文档",
            notes = "获取全部文档",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getMainByPage(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = this.mainFacade.getMainByPage(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(records));
    }

    @RequestMapping(value = "/getMainByCompanyId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "根据公司id获取文档",
            notes = "根据公司id获取文档",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{companyId:'2017121617484BB9BEB4A635E7458410'}", dataType = "String", paramType = "body")
    public void getMainByCompanyId(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = this.mainFacade.getMainByCompanyId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
    }

    @RequestMapping(value = "/getCompanyListByUserId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取公司列表",
            notes = "获取公司列表",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:'201711271733404C9DA1461F2421F828'}", dataType = "String", paramType = "body")
    public void getCompanyListByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = this.mainFacade.getCompanyListByUserId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
    }

    @RequestMapping(value = "/getMainDetail.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取文档详情",
            notes = "获取文档详情",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{id:'20180426174242B583902F4B1B385E40'}", dataType = "String", paramType = "body")
    public void getMainDetail(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = this.mainFacade.getMainDetail(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(records));
    }

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "增加修改文档",
            notes = "增加修改文档",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{\"docMain\":{fkTypeId: '20180423190945C4BFA747C7648C57DF'," +
            "docName:'测试doc',source:'1',delFlag:0,attachName:'222',attachUrl:'333',imageName:'444',imageUrl:'555'," +
            "companyId:'2017121617464DFEB683A82815924552',departName:'销售部',screenType:'0'}," +
            "\"attachHistoryList\":[{attachName:'333',attachUrl:'444'},{attachName:'555',attachUrl:'666'}]}",
            dataType = "String", paramType = "body")
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.mainFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "删除文档",
            notes = "删除文档",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{id:'20180426174242B583902F4B1B385E40'}", dataType = "String", paramType = "body")
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = mainFacade.delById(jsonParam.getString("id"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("删除失败！"));
        }
    }
}